import utils from './utils.js';
import constants from '@/utils/constants.js';
import store from '@/store/index.js';
export function processResponse(scope, pageComponent, response, children) {
	if (response.code == 1) {
		var viewPropMap = response.viewPropMap;
		for (var k in viewPropMap) {
			if (pageComponent.componentModel.id == k) {
				pageComponent.setProp(viewPropMap[k]);
				continue;
			}
			for (var i = 0; i < children.length; i++) {
				if (children[i] != null && (children[i].componentModel.id == k ||
						children[i].componentModel.backupId == k)) {
					if (children[i]["setProp"] != undefined) {
						children[i].setProp(viewPropMap[k]);
					}
				}
				//break;
			}
		}
	}
	processBaseResponse(scope, pageComponent, response, children);
}

export function processBaseResponse(scope, pageComponent, response, children) {
	if (response.code == 1) {
		var clientScriptList = response.clientScriptList;
		if (clientScriptList != undefined && clientScriptList != null) {
			for (var i = 0; i < clientScriptList.length; i++) {
				var clientScript = clientScriptList[i];
				if (clientScript.type == "navigatePageClientScript") {
					pageComponent.navigatePage(clientScript.pageCode, clientScript.paraMap);
				} else if (clientScript.type == "tableAddRowClientScript") {
					for (i = 0; i < children.length; i++) {
						if (children[i].componentModel.id == clientScript.tableId && children[i].addRow != undefined) {
							children[i].addRow(clientScript.data);
						}
					}
				} else if (clientScript.type == "tableRemoveRowClientScript") {
					for (i = 0; i < children.length; i++) {
						if (children[i].componentModel.id == clientScript.tableId && children[i].removeRow !=
							undefined) {
							children[i].removeRow();
						}
					}
				} else if (clientScript.type == "searchDialogCallbackClientScript") {
					var parent = pageComponent;
					while (parent.setSelectedValue == undefined) {
						parent = parent.$parent;
					}
					parent.setSelectedValue(clientScript.selectedValue);
					parent.setVisible(false);
				} else if (clientScript.type == "searchDisplayClientScript") {
					for (i = 0; i < children.length; i++) {
						if (children[i].componentModel.id == clientScript.componentId && children[i].searchCallback !=
							undefined) {
							children[i].searchCallback(clientScript.value, clientScript.display);
							break;
						}
					}
				} else if (clientScript.type == "treeAddNodeClientScript") {
					for (i = 0; i < children.length; i++) {
						if (children[i].componentModel.id == clientScript.treeId && children[i].addTreeNode !=
							undefined) {
							children[i].addTreeNode({
								id: clientScript.id,
								name: clientScript.name
							});
							break;
						}
					}
				} else if (clientScript.type == "treeUpdateNodeClientScript") {
					for (i = 0; i < children.length; i++) {
						if (children[i].componentModel.id == clientScript.treeId && children[i].updateTreeNode !=
							undefined) {
							children[i].updateTreeNode({
								id: clientScript.id,
								name: clientScript.name
							});
							break;
						}
					}
				} else if (clientScript.type == "treeRemoveNodeClientScript") {
					for (i = 0; i < children.length; i++) {
						if (children[i].componentModel.id == clientScript.treeId && children[i].removeTreeNode !=
							undefined) {
							children[i].removeTreeNode({
								id: clientScript.id,
								name: clientScript.name
							});
							break;
						}
					}
				} else if (clientScript.type == "mListViewAddRowClientScript") {
					for (i = 0; i < children.length; i++) {
						if (children[i].componentModel.id == clientScript.listViewId && children[i].addRow !=
							undefined) {
							children[i].addRow(clientScript.data);
						}
					}
				} else if (clientScript.type == "mListViewRemoveRowClientScript") {
					for (i = 0; i < children.length; i++) {
						if (children[i].componentModel.id == clientScript.listViewId && children[i].removeRow !=
							undefined) {
							children[i].removeRow();
							break;
						}
					}
				} else if (clientScript.type == "mListViewUpdateRowClientScript") {
					for (i = 0; i < children.length; i++) {
						if (children[i].componentModel.id == clientScript.listViewId && children[i].updateRow !=
							undefined) {
							children[i].updateRow(clientScript.data);
							break;
						}
					}
				} else if (clientScript.type == "buttonParaCallback") {
					parent = pageComponent;
					while (parent.onParaCallback == undefined) {
						parent = parent.$parent;
					}
					parent.onParaCallback();
				} else if (clientScript.type == "jswindow") {
					window.open(constants.ROOT_URL + clientScript.url);
				} else if (clientScript.type == "clientPageOpenClickScript") {
					if (clientScript.env == "dev") {
						store.state.webIDE.$refs.mainPageContainer.loadPage({
							"name": clientScript.name,
							"menuType": "clientpage",
							"pageCode": clientScript.pageCode
						}, clientScript.paraMap);
					}
				}
			}
		}

		var messageList = response.messageList;
		if (messageList != null && messageList.length > 0) {
			utils.showMessageDialog(scope, '提示', messageList[0].message);
		}
	} else if (response.code == 2) {
		scope.$router.push("devlogin");
	} else {
		utils.showMessageDialog(scope, '出错了', response.message);
	}
}

export function getPageSubmitData(pageComponent, children) {
	var data = {};
	for (var i = 0; i < children.length; i++) {
		if (children[i].componentModel == undefined) continue;
		if (utils.startWith(children[i].componentModel.id, pageComponent.componentModel.code)) {
			if (children[i]["getValue"] != undefined) {
				var value = children[i]["getValue"]();
				if (value != undefined) {
					data[children[i].componentModel.id] = value;
				}
			} else if (children[i]["getState"] != undefined) {
				var state = children[i]["getState"]();
				if (state != undefined) {
					for (var k in state) {
						data[k] = state[k];
					}
				}
			}
		}
	}
	return data;
}

export function getPageData(pageComponent, children) {
	var data = {};
	for (var i = 0; i < children.length; i++) {
		if (children[i].componentModel == undefined) continue;
		if (utils.startWith(children[i].componentModel.id, pageComponent.componentModel.code)) {
			if (children[i]["getValue"] != undefined) {
				var value = children[i]["getValue"]();
				if (value != undefined) {
					data[children[i].componentModel.dataField] = value;
				}
			}
		}
	}
	return data;
}
